package com.yfuxdeer.youchat.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.donkingliang.imageselector.utils.ImageSelector;
import com.yfuxdeer.youchat.service.MomentService;
import com.yfuxdeer.youchat.R;
import com.yfuxdeer.youchat.pojo.Moment;
import com.yfuxdeer.youchat.pojo.User;
import com.yfuxdeer.youchat.service.FriendService;
import com.yfuxdeer.youchat.service.UserService;
import com.yfuxdeer.youchat.utils.ConstellationUtil;
import com.yfuxdeer.youchat.utils.ImageUtil;
import com.yfuxdeer.youchat.utils.StorageUtil;
import com.yfuxdeer.youchat.utils.Store;
import com.yfuxdeer.youchat.utils.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class UserinfoActivity extends AppCompatActivity {

    private static final String TAG = UserinfoActivity.class.getSimpleName();
    private static final int REQUEST_CODE = 1365;
    User user;
    private Store store = Store.getInstance();
    private ImageView head;
    private TextView nickName;
    private TextView remark;
    private TextView username;
    private TextView figure;
    private TextView gender;
    private TextView age;
    private TextView birthday;
    private TextView constellation;
    private TextView school;
    private TextView address;
    private TextView occupation;
    private TextView email;
    private TextView phone;
    private TextView moment;
    private TextView time;
    private Button btn;
    private Button cancel;
    private Button modifyRemark;
    private EditText modifyContext;
    private PopupWindow popupWindow;
    private String remark_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);

        //注册广播
        registerReceiver(updateUserReceiver, new IntentFilter("com.yfuxdeer.youchat.updateUserReceiver"));
        registerReceiver(findUserReceiver, new IntentFilter("com.yfuxdeer.youchat.findUserReceiver"));
        registerReceiver(findLastMomentReceiver, new IntentFilter("com.yfuxdeer.youchat.findLastMomentReceiver"));
        registerReceiver(modifyRemarkReceiver, new IntentFilter("com.yfuxdeer.youchat.modifyRemarkReceiver"));

        findViews();

        Intent intent = getIntent();
        if (intent != null) {
            String user_id = intent.getStringExtra("user_id");
            remark_text = intent.getStringExtra("remark");
            if (user_id.equals(store.getCurrentUser().getUsername())) {
                user = store.getCurrentUser();
                Log.e(TAG, "onCreate: " + user);
                //自己的可以直接初始化
                init();
            } else {
                //调用服务完成查询
                Intent serviceIntent = new Intent(getApplicationContext(), UserService.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("method", UserService.METHOD.FIND_USER);
                bundle.putString("user_id", user_id);
                serviceIntent.putExtras(bundle);
                startService(serviceIntent);
            }
        }
    }

    /**
     * onActivityResult 对拍照、相册选择图片的返回结果进行处理
     *
     * @param requestCode 返回码，用于确定是哪个 Activity 返回的数据
     * @param resultCode  返回结果，一般如果操作成功返回的是 RESULT_OK
     * @param data        返回对应 activity 返回的数据
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: " + "有返回结果");
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    //获取选择器返回的数据
                    List<String> images = data.getStringArrayListExtra(ImageSelector.SELECT_RESULT);
                    Log.e(TAG, "onActivityResult: " + images.get(0));
                    ImageUtil.upload(this, images.get(0));
                    store.cacheState++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //自己的头像
                    Log.e(TAG, "onActivityResult: " + "获取： " + store.cacheState);
                    ImageUtil.glideLoad(getApplicationContext(), user.getUsername(), store.cacheState, head);
                    //修改头像之后清理一次缓存
                    sendBroadcast(new Intent("com.yfuxdeer.youchat.clearCacheReceiver"));
                }
            }
        }
    }


    private void setListeners() {
        head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view_ = getLayoutInflater().inflate(R.layout.pop_modify_head, null);
                popupWindow = new PopupWindow(view_, ViewGroup.LayoutParams.MATCH_PARENT
                        , ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setFocusable(true);
                popupWindow.showAsDropDown(view);
                Button camera = view_.findViewById(R.id.useCamera);
                Button album = view_.findViewById(R.id.fromAlbum);

                camera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ImageSelector.builder()
                                .useCamera(true) // 设置使用拍照
                                .setCrop(true) //设置裁剪
                                .setCropRatio(1.0f) //设置裁剪比例
                                .setSingle(true)  //设置单选
                                .start(UserinfoActivity.this, REQUEST_CODE); // 打开相册
                        popupWindow.dismiss();
                    }
                });

                album.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ImageSelector.builder()
                                .useCamera(false) // 设置不使用拍照
                                .setCrop(true) //设置裁剪
                                .setCropRatio(1.0f) //设置裁剪比例
                                .setSingle(true)  //设置单选
                                .start(UserinfoActivity.this, REQUEST_CODE); // 打开相册
                        popupWindow.dismiss();
                    }
                });

            }
        });

        //修改昵称
        remark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改昵称", user.getNickName(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String nickName = modifyContext.getText().toString();
                        if (nickName.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (nickName.length() > 45) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        //暂时修改本页信息显示
                        user.setNickName(nickName);
                        remark.setText(nickName);
                        UserinfoActivity.this.nickName.setText(nickName);

                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);

                        //关闭窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });
        nickName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改昵称", user.getNickName(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String nickName = modifyContext.getText().toString();
                        if (nickName.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (nickName.length() > 45) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setNickName(nickName);
                        remark.setText(nickName);
                        UserinfoActivity.this.nickName.setText(nickName);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        // 修改签名
        figure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改签名", figure.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String figure = modifyContext.getText().toString();
                        if (figure.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (figure.length() > 180) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setFigure(figure);
                        UserinfoActivity.this.figure.setText(figure);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改性别
        gender.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改性别", gender.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String gender = modifyContext.getText().toString();
                        if (!(gender.equals("男") || gender.equals("女"))) {
                            Toast.makeText(getApplicationContext(), "请输入男或女", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setGender(gender);
                        UserinfoActivity.this.gender.setText(gender);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改年龄
        age.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "你可以修改生日,将自动计算年龄", Toast.LENGTH_SHORT).show();
            }
        });

        //修改生日
        birthday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view_ = getLayoutInflater().inflate(R.layout.pop_modify_time, null);
                popupWindow = new PopupWindow(view_, ViewGroup.LayoutParams.MATCH_PARENT
                        , ViewGroup.LayoutParams.MATCH_PARENT);
                popupWindow.setFocusable(true);
                popupWindow.showAsDropDown(view);
                DatePicker datePicker = view_.findViewById(R.id.time_picker);
                datePicker.init(user.getBirthday().getYear() + 1900,
                        user.getBirthday().getMonth() + 1, user.getBirthday().getDate(), null);
                datePicker.setMaxDate(new Date().getTime());

                Button btn = view_.findViewById(R.id.confirm);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String birthday_ = datePicker.getYear() + "-" + (datePicker.getMonth() + 1) + "-" + datePicker.getDayOfMonth();
                        //暂时修改本页信息
                        //修改生日
                        Log.e(TAG, "onClick: " + DateUtil.getDate(birthday_ + " 00:00"));
                        user.setBirthday(DateUtil.getDate(birthday_ + " 00:00"));
                        Log.e(TAG, "onClick: " + user.getBirthday());
                        birthday.setText(birthday_);
                        //计算年龄
                        int age_ = new Date().getYear() - user.getBirthday().getYear();
                        user.setAge(age_);
                        age.setText(String.valueOf(age_));
                        //计算星座
                        String cs = ConstellationUtil.getConstellation(user.getBirthday());
                        user.setConstellation(cs);
                        constellation.setText(cs);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改星座
        constellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "你可以修改生日,将自动计算星座", Toast.LENGTH_SHORT).show();
            }
        });

        //修改学校
        school.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改学校", school.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String school = modifyContext.getText().toString();
                        if (school.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (school.length() > 45) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setSchool(school);
                        UserinfoActivity.this.school.setText(school);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改住址
        address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改住址", address.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String address = modifyContext.getText().toString();
                        if (address.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (address.length() > 90) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setAddress(address);
                        UserinfoActivity.this.address.setText(address);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改职业
        occupation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改职业", occupation.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String occupation = modifyContext.getText().toString();
                        if (occupation.equals("")) {
                            Toast.makeText(getApplicationContext(), "输入为空", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (occupation.length() > 18) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setOccupation(occupation);
                        UserinfoActivity.this.occupation.setText(occupation);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改邮箱
        email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改邮箱", email.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String email = modifyContext.getText().toString();
                        if (!validateEmail(email)) {
                            Toast.makeText(getApplicationContext(), "邮箱地址有误", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (email.length() > 45) {
                            Toast.makeText(getApplicationContext(), "内容太长", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setEmail(email);
                        UserinfoActivity.this.email.setText(email);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

        //修改电话
        phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popModify(R.layout.pop_modify, view, "修改电话", phone.getText().toString(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String phone = modifyContext.getText().toString();
                        if (!validatePhone(phone)) {
                            Toast.makeText(getApplicationContext(), "手机号格式有误", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //暂时修改本页信息
                        user.setPhone(phone);
                        UserinfoActivity.this.phone.setText(phone);
                        //启动服务修改数据库
                        Intent intent = new Intent(getApplicationContext(), UserService.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("method", UserService.METHOD.UPDATE);
                        bundle.putSerializable("user", user);
                        intent.putExtras(bundle);
                        startService(intent);
                        //关闭修改窗口
                        popupWindow.dismiss();
                    }
                });
            }
        });

    }

    private boolean validateEmail(String email) {
        return Pattern.matches("^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", email);
    }

    private boolean validatePhone(String phone) {
        return Pattern.matches("^1[0-9]{10}$", phone);
    }

    private void init() {
        if (user.getUsername().equals(store.getCurrentUser().getUsername())) {
            //自己的头像
            ImageUtil.glideLoad(getApplicationContext(), user.getUsername(), store.cacheState, head);
        } else {
            //好友的头像
            ImageUtil.glideLoad(getApplicationContext(), user.getUsername(), 0, head);
        }
        remark.setText(user.getNickName());
        nickName.setText("昵称：" + user.getNickName());
        username.setText(user.getUsername());
        figure.setText(user.getFigure());
        gender.setText(user.getGender());
        age.setText(String.valueOf(user.getAge()));
        birthday.setText(DateUtil.getDate(user.getBirthday()));
        constellation.setText(user.getConstellation());
        school.setText(user.getSchool());
        address.setText(user.getAddress());
        occupation.setText(user.getOccupation());
        email.setText(user.getEmail());
        phone.setText(user.getPhone());
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (user.getUsername().equals(store.getCurrentUser().getUsername())) {  //当前用户
            setListeners(); //为当前用户添加监听事件
            btn.setClickable(false);
            btn.setTextColor(Color.rgb(.3f, .3f, .3f));
            btn.setBackgroundColor(Color.rgb(.5f, .5f, .5f));
            modifyRemark.setClickable(false);
            modifyRemark.setBackgroundColor(Color.argb(1.0f, 0.5f, 0.5f, 0.5f));
        } else {  //好友
            //修改为备注
            this.remark.setText(remark_text);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getApplicationContext(), ChatActivity.class);
                    intent.putExtra("nickName", UserinfoActivity.this.remark.getText());
                    intent.putExtra("friend_id", user.getUsername());
                    startActivity(intent);
                    finish();
                }
            });
            modifyRemark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popModify(R.layout.pop_modify, view, "修改备注", UserinfoActivity.this.remark.getText().toString(), new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String remark_ = modifyContext.getText().toString();
                            if (remark_.equals("")) {
                                Toast.makeText(getApplicationContext(), "输入不能为空", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            //调用服务
                            Intent intent = new Intent(getApplicationContext(), FriendService.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("method", FriendService.METHOD.MODIFY_REMARK);
                            bundle.putString("host_id", store.getCurrentUser().getUsername());
                            bundle.putString("friend_id", user.getUsername());
                            bundle.putString("remark", remark_);
                            intent.putExtras(bundle);
                            startService(intent);

                            popupWindow.dismiss();
                        }
                    });
                }
            });
        }
        initMoment();
    }

    private void initMoment() {
        //调用服务请求网络 查询最近的一条动态
        Intent intent = new Intent(getApplicationContext(), MomentService.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("method", MomentService.METHOD.FINDLAST);
        bundle.putSerializable("username", user.getUsername());
        intent.putExtras(bundle);
        startService(intent);
    }

    private void findViews() {
        head = findViewById(R.id.head);
        nickName = findViewById(R.id.nickName);
        remark = findViewById(R.id.modify_context);
        username = findViewById(R.id.id);
        figure = findViewById(R.id.figure);
        gender = findViewById(R.id.gender);
        age = findViewById(R.id.age);
        birthday = findViewById(R.id.birthday);
        constellation = findViewById(R.id.constellation);
        school = findViewById(R.id.school);
        address = findViewById(R.id.address);
        occupation = findViewById(R.id.occupation);
        email = findViewById(R.id.email);
        phone = findViewById(R.id.phone);
        btn = findViewById(R.id.btn);
        cancel = findViewById(R.id.cancel);
        modifyRemark = findViewById(R.id.modifyRemark);
        moment = findViewById(R.id.moment);
        time = findViewById(R.id.time);
    }

    private void popModify(int layoutId, View view, String title_, String hint, View.OnClickListener listener) {
        //加载弹出层
        View view_ = getLayoutInflater().inflate(layoutId, null);
        popupWindow = new PopupWindow(view_, ViewGroup.LayoutParams.MATCH_PARENT
                , ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(view);
        Button modify = view_.findViewById(R.id.modify);
        Button cancel = view_.findViewById(R.id.cancel);
        TextView title = view_.findViewById(R.id.title);
        title.setText(title_);
        //取消按钮的点击事件
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        modifyContext = view_.findViewById(R.id.modify_context);
        modifyContext.setHint(hint);
        //修改按钮点击事件
        modify.setOnClickListener(listener);
    }

    /**
     * 修改用户的广播接受者
     */
    private BroadcastReceiver updateUserReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.e(TAG, "onReceive: updateUserReceiver");
            Bundle bundle = intent.getExtras();
            UserService.RESULT result = (UserService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    //更新缓存
                    StorageUtil.writeUser(getApplicationContext(), user);
                    store.setCurrentUser(user);
                    Toast.makeText(UserinfoActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    break;
                case NETWORK_EXCEPTION:
                    //还原回去
                    user = store.getCurrentUser();
                    Toast.makeText(UserinfoActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
            init();
        }
    };

    private BroadcastReceiver findUserReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            UserService.RESULT result = (UserService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    user = (User) bundle.getSerializable("user");
                    init();  //请求成功了才调用Init方法
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(UserinfoActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private BroadcastReceiver findLastMomentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            MomentService.RESULT result = (MomentService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    Moment moment = (Moment) bundle.getSerializable("moment");
                    if (moment == null) {
                        //说明用户没有发信息
                        UserinfoActivity.this.moment.setText("该用户没有发表过动态");
                        UserinfoActivity.this.time.setText("");
                    } else {
                        UserinfoActivity.this.moment.setText(moment.getContent());
                        time.setText(DateUtil.getDate(moment.getTime()));
                    }
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

    private BroadcastReceiver modifyRemarkReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            FriendService.RESULT result = (FriendService.RESULT) bundle.getSerializable("result");
            switch (result) {
                case SUCCESS:
                    remark.setText(bundle.getString("remark"));
                    Toast.makeText(getApplicationContext(), "修改成功", Toast.LENGTH_SHORT).show();
                    break;
                case NETWORK_EXCEPTION:
                    Toast.makeText(getApplicationContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}

